﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EmoneyWeiXinEntity;
using EmoneyWeiXinCommon;
using System.Data.SqlClient;
using System.Data;
namespace EmoneyWeiXinDAL
{
    public class WXUserDal
    {
        public static List<WXUserEntity> getWXUserList(int pindex, int psize, int weixinid, out int total, string nickname, int sex, string province, string city, string subscribe1, string subscribe2, int groupid, string labelids, string subStatu)
        {
            SqlParameter[] parameters = new SqlParameter[] {
            new SqlParameter("@pindex",pindex),
            new SqlParameter("@psize", psize),
            new SqlParameter("@total",0),
            new SqlParameter("@weixinUserid",weixinid),
            new SqlParameter("@nickname", nickname),
            new SqlParameter("@sex", sex),
            new SqlParameter("@province", province),
            new SqlParameter("@city", city),
            new SqlParameter("@subscribe1", subscribe1),
            new SqlParameter("@subscribe2", subscribe2),
            new SqlParameter("@groupid", groupid),
            new SqlParameter("@labelids", labelids),
            new SqlParameter("@subStatu",subStatu)
            };
            parameters[2].Direction = ParameterDirection.Output;
            List<WXUserEntity> result = SQLHelper.GetProcedureDataByColumnsLst<WXUserEntity>(Config.EmoneyWeiXin, "sp_WXUser_Select", parameters);
            total = Convert.ToInt32(parameters[2].Value);
            return result;
        }
        public static int UpdateWXUserGroup(string ids, int groupid)
        {
            SqlParameter[] parameters = new SqlParameter[] {
             new SqlParameter("@ids" , ids),
             new SqlParameter("@groupid" , groupid)
            };
            int result = SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, CommandType.StoredProcedure, "sp_WXUser_UpdateGroup", parameters);
            return result;
        }

        public static WXUserEntity GetWxuserByOpenId(string openId)
        {
            string sql = @"SELECT TOP 1 T1.openid,weixinUserid,nickname,t2.QQNumber,t2.phoneHideMid from WXUser T1 with(nolock) 
                           LEFT JOIN UserPhoneBind t2 with(nolock) ON T1.openid=t2.openId
                           WHERE T1.openid=@openid";
            SqlParameter[] paras =
            {
              new SqlParameter("@openid",openId)
            };
            return SQLHelper.GetDataByColumns<WXUserEntity>(Config.EmoneyWeiXin, sql, paras);
        }

        public static WXUserEntity GetWxuserByOpenId2(string openId)
        {
            string sql = @"SELECT [wxid]
      ,[openid]
      ,[weixinUserid]
      ,[nickname]
      ,[sex]
      ,[language]
      ,[country]
      ,[province]
      ,[city]
      ,[headimgurl]
      ,[subscribe_time]
      ,[unionid]
      ,[createtime]
      ,[updatetime]
      ,[subscribe]
      ,[VIP]
      ,[groupid]
      ,[labelid]
  FROM [EmoneyWeiXin].[dbo].[WXUser] where openid=@openid";
            SqlParameter[] paras =
            {
              new SqlParameter("@openid",openId)
            };
            return SQLHelper.GetDataByColumns<WXUserEntity>(Config.EmoneyWeiXin, sql, paras);
        }

        public static int getWXUserStat(int weixinid, out int subscribeCount, out int unsubscribeCount, out int subscribeTotalCount)
        {
            SqlParameter[] parameters = new SqlParameter[] {
            new SqlParameter("@weixinid",weixinid),
            new SqlParameter("@subscribeCount", 0),
            new SqlParameter("@unsubscribeCount", 0),
            new SqlParameter("@subscribeTotalCount", 0)
            };
            parameters[1].Direction = ParameterDirection.Output;
            parameters[2].Direction = ParameterDirection.Output;
            parameters[3].Direction = ParameterDirection.Output;
            var obj = SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, CommandType.StoredProcedure, "sp_WXUser_Select_stat", parameters);
            subscribeCount = Convert.ToInt32(parameters[1].Value);
            unsubscribeCount = Convert.ToInt32(parameters[2].Value);
            subscribeTotalCount = Convert.ToInt32(parameters[3].Value);
            return obj;
        }
    }
}
